package com.example.eldersystem.controller;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.SystemUser;
import com.example.eldersystem.operatedata.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/systemuser")
public class SystemUserController {

    @Autowired
    SystemUserService systemUserService;

    @RequestMapping("/add")
    private DataResponse<String> addSystemUser(@RequestBody Map<String,Object> map){

        DataResponse<String> dataResponse = new DataResponse<>();
        SystemUser systemUser = new SystemUser();
        systemUser.setUserName(map.get("userName").toString());
        systemUser.setPassword(map.get("password").toString());
        systemUser.setPhone(map.get("phone").toString());
        systemUser.setRealName(map.get("realName").toString());
        systemUser.setEmail(map.get("email").toString());
        systemUser.setDescription(map.get("description").toString());
        systemUser.setSex(map.get("sex").toString());
        dataResponse = systemUserService.addSystemUser(systemUser);
        return dataResponse;
    }

    @RequestMapping("/update")
    private DataResponse<String> updateSystemUser(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        SystemUser systemUser = new SystemUser();
        dataResponse.setCode(6);
        systemUser.setId(Integer.parseInt(map.get("id").toString()));
        systemUser.setUserName(map.get("userName").toString());
        systemUser.setPassword(map.get("password").toString());
        systemUser.setPhone(map.get("phone").toString());
        systemUser.setRealName(map.get("realName").toString());
        systemUser.setEmail(map.get("email").toString());
        systemUser.setDescription(map.get("description").toString());
        systemUser.setSex(map.get("sex").toString());
        dataResponse = systemUserService.updateSystemUser(systemUser);
        return dataResponse;
    }

    @RequestMapping("/delete")
    private DataResponse<String> deleteSystemUser(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        int id = Integer.parseInt(map.get("id").toString());
        dataResponse=systemUserService.deleteSystemUser(id);
        return dataResponse;
    }

    @RequestMapping("/select")
    private DataResponse<List<SystemUser>> getSystemUser(@RequestBody Map<String,Object> map){
        DataResponse<List<SystemUser>> dataResponse = new DataResponse<>();
        int from = 0;
        int num = 0;
        try{
            from = Integer.parseInt(map.get("from").toString());
            num = Integer.parseInt(map.get("num").toString());
        }catch(Exception e){
            dataResponse.setMsg("请求错误");
            dataResponse.setCode(1);
            return dataResponse;
        }
        dataResponse = systemUserService.selectSystemUser(from,num);
        return dataResponse;
    }

    @RequestMapping("/search")
    private DataResponse<List<SystemUser>> searchSystemUser(@RequestBody Map<String,Object> map){
        DataResponse<List<SystemUser>> dataResponse = new DataResponse<>();
        String realName;
        try{
            realName = map.get("realName").toString();
        }catch(Exception e){
            dataResponse.setMsg("请求错误");
            dataResponse.setCode(1);
            return dataResponse;
        }
        dataResponse = systemUserService.selectSystemUserByRealName(realName);
        return dataResponse;
    }

    @RequestMapping("/count")
    private  DataResponse<Integer> countSystemUser(){
        DataResponse<Integer> dataResponse = new DataResponse<>();
        int num = systemUserService.getSystemUserNum();
        dataResponse.setCode(0);
        dataResponse.setData(num);
        return dataResponse;
    }
}
